package com.laoda.ikun_food.controller.web.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoda.ikun_food.entity.Food;
import com.laoda.ikun_food.service.FoodService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
@RestController
@RequestMapping("/api/user")
@AllArgsConstructor
public class HomeRestController {

    private final FoodService foodService;

    @GetMapping("/foodList")
    public IPage<Food> showAllSights(
            @RequestParam(name="foodname",defaultValue = "", required = false) String searchName,
            @RequestParam(name="photo",defaultValue = "0", required = false) BigDecimal lowPrice,
            @RequestParam(name="price",defaultValue = "0", required = false) BigDecimal hiPrice,
            @RequestParam(name = "page", defaultValue = "1", required = false) int page,
            @RequestParam(name = "size", defaultValue = "10", required = false) int size
    ){
        IPage<Food> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(size);

        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        if(searchName != null && !searchName.isEmpty()){
            wrapper.like("foodname", searchName);
        }
        if(lowPrice != null && !lowPrice.equals(BigDecimal.ZERO)){
            wrapper.ge("price", lowPrice);
        }
        if(hiPrice != null && !hiPrice.equals(BigDecimal.ZERO)){
            wrapper.le("price", hiPrice);
        }
        return foodService.page(pager, wrapper);
    }

}
